<template>
  <div>
    <h1>添加管理员</h1>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="名称" prop="username">
        <el-input v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item label="简介" prop="description">
        <el-input v-model="ruleForm.description"></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="ruleForm.nickname"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="ruleForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="头像" prop="avatar">
        <el-input v-model="ruleForm.avatar"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="ruleForm.email"></el-input>
      </el-form-item>
      <el-form-item label="角色" prop="roleIds">
        <el-select v-model="ruleForm.roleIds" multiple placeholder="请选择">
         <el-option
            v-for="item in roleListOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
       </el-select>
      </el-form-item>
      <el-form-item label="是否启用" prop="enable">
        <el-switch
            v-model="ruleForm.enable"
            active-value="1"
            inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ff4949">
        </el-switch>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      roleListOptions: [],
      ruleForm: {
        roleIds: [],
        username: '',
        password:'',
        description: '',
        nickname: '',
        phone:'',
        avatar:'',
        email:'',
        enable:''
      },
      rules: {
        username: [
          {required: true, message: '请输入管理员名称', trigger: 'blur'},
          {min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入管理员密码', trigger: 'blur'},
          {min: 4, max: 30, message: '长度在 4 到 30 个字符', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    loadRoleList(){
      console.log('准备从服务器端加载【角色列表】……');
      let url='http://localhost:9999/roles';
      console.log('请求路径：'+url);
      this.axios
          .create({'headers':{'Authorization':localStorage.getItem('jwt')}})
          .get(url)
          .then((response)=>{
            let responseData=response.data;
            console.log('接收到服务器端响应的结果：');
            console.log(responseData);
            this.roleListOptions=responseData.data;
          })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert('您输入的用户名和密码的格式是正确的，但是，登录功能我们还没做!');
          let url = 'http://localhost:9999/admins/add-new';
          console.log('尝试添加管理员……');
          console.log('请求路径为：' + url);
          console.log('请求参数为：' + this.ruleForm);
          console.log(this.ruleForm);
          let formData = this.qs.stringify(this.ruleForm,{'arrayFormat':'repeat'});
          console.log('将ruleForm对象转换为FormData：')
          console.log(formData);
          let jwt=localStorage.getItem("jwt");
          console.log('从LocalStorage中取出的JWT：');
          console.log(jwt);
          this.axios
                    .create({'headers':{'Authorization':localStorage.getItem("jwt")}})
                    .post(url, formData).then((response) => {
            console.log('服务器端响应的结果：' + response);
            console.log(response.data);
            let responseData = response.data;
            if (responseData.state == 20000) {
              console.log('添加管理员成功');
              this.$message({
                message: '添加管理员成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              console.log(responseData.message);
              this.$notify.error({
                title: '添加管理员失败',
                message: responseData.message
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.loadRoleList();
  }
}
</script>

<style scoped>

</style>